package com.ssm.controller;  
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import com.ssm.util.CommonUtils;
import com.ssm.util.Page;



public class BaseController {  
    public static String scanName = null;
    
    protected static final String ERROR_MSG_KEY = "errorMsg";  
    
    private static final String userContext = "username";
    
    private static final String password = "password";
 
    //① 获取保存在Session中的用户对象  
    protected String getSessionUser(HttpServletRequest request,HttpSession session) {  
        return CommonUtils.isEmpty(request.getParameter("uname")) ? (String)session.getAttribute(userContext) : request.getParameter("uname");  
    }  
     
     //②获取用户密码
    protected String getSessionPwd(HttpServletRequest request,HttpSession session) {  
        return CommonUtils.isEmpty(request.getParameter(password)) ? (String)session.getAttribute(password) : request.getParameter(password);  
    }  
      
    //③ 获取基于应用程序的url绝对路径  
    public final String getAppbaseUrl(HttpServletRequest request, String url) {  
        Assert.hasLength(url, "url不能为空");  
        Assert.isTrue(url.startsWith("/"), "必须以/打头");  
        return request.getContextPath() + url;  
    }  
    
    /**
     * 获取第几页 
     * @param request
     */
    public final int getPageNo(HttpServletRequest request){
        String pageNoStr = (String)request.getParameter("pageNo");
        int pageNo = StringUtils.isEmpty(pageNoStr) ? 1 : Integer.valueOf(pageNoStr);
        return pageNo;
    }
    
    /**
     * 获取请求参数map  封装page对象
     * @param request
     */
    public Map<String,Object> getRequestParams(HttpServletRequest request){
        Map<String, Object> params = new HashMap<String,Object>();
        Map<String, String[]> parameterMap = request.getParameterMap();
        if (parameterMap != null) 
        {
            for (String key : parameterMap.keySet()) 
            {
                String[] values = parameterMap.get(key);
                params.put(key, values.length == 1 ? values[0].trim() : values);
            }
        }
        int pageNo = getPageNo(request);
        //调用方法名称 分页请求地址
        String url = Thread.currentThread().getStackTrace()[2].getMethodName();
        Page page = Page.newBuilder(pageNo,getPageSize(request),url);
        params.put("page", page);
        return params;
    }  
    
    /**
     * 获取请求参数map  封装page对象
     * @param request
     */
    public Map<String,Object> getPageParams(HttpServletRequest request){
    	Map<String, Object> params = new HashMap<String,Object>();
    	int pageNo = getPageNo(request);
    	//调用方法名称 分页请求地址
    	String url = Thread.currentThread().getStackTrace()[2].getMethodName();
    	Page page = Page.newBuilder(pageNo,getPageSize(request),url);
    	params.put("page", page);
    	return params;
    }  
    
    /**
     * 获取第几页 
     * @param request
     */
    public final int getPageSize(HttpServletRequest request){
        String pageSizeStr = (String)request.getParameter("pageSize");
        int pageSize = StringUtils.isEmpty(pageSizeStr) ? Integer.valueOf(Page.DEFAULT_PAGESIZE) : Integer.valueOf(pageSizeStr);
        return pageSize;
    }
      
}  